package com.smartinput.auth.application.service

import com.smartinput.auth.application.dto.*
import com.smartinput.user.application.service.UserService
import com.smartinput.security.JwtService
import org.slf4j.LoggerFactory
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service

@Service
class AuthService(
    private val userService: UserService,
    private val passwordEncoder: PasswordEncoder,
    private val jwtService: JwtService
) {
    
    private val logger = LoggerFactory.getLogger(AuthService::class.java)

    fun register(request: RegisterRequest): RegisterResponse {
        return userService.register(request.userName, request.password)
    }

    fun login(request: LoginRequest): LoginResponse {
        return userService.login(request.userName, request.password)
    }

    fun validateCurrentToken(): Boolean {
        return try {
            val authentication = org.springframework.security.core.context.SecurityContextHolder.getContext().authentication
            if (authentication != null && authentication.isAuthenticated && authentication.name != null) {
                logger.info("Token验证成功，用户ID: ${authentication.name}")
                true
            } else {
                logger.warn("Token验证失败：认证信息无效")
                false
            }
        } catch (e: Exception) {
            logger.error("Token验证异常: ${e.message}")
            false
        }
    }
} 